package com.gofaraway.service.student.leetcode.sort;

import java.util.Arrays;

/**
 * @author ChenPeng
 * @Date 2024/9/8 15:57
 */
public class 排序算法 {

    public static void main(String[] args) {
        int[] a = new  int[]{3,5,1, 90, 33, 35};
        bubbleSort(a);

        System.out.println(Arrays.toString(a));
    }


    /**
     * 冒泡排序
     * @param a 待排序的数组
     * @param n 数组长度
     */
    public static void bubbleSort(int[] a ) {
        int n = a.length;
        if (n <= 1) {
            return;
        }

        boolean flag = false;//提前撤出冒泡排序标志位
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j +1]) {
                    int temp = a[j];
                    a[j] = a[j +1];
                    a[j +1] = temp;
                    flag = true;
                }
            }
            if (!flag) break;
        }
    }
}
